#ifndef KCP_COMMON_H
#define KCP_COMMON_H

#include "ikcp.h"
#include <stdint.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <stdbool.h>
#include <functional>
#include <unistd.h>

#define KCP_MAX_WAIT_PACKETS 1000
#define KCP_RECV_BUFFER_SIZE 1500

typedef struct stKcpConfig
{
    int32_t local_port;
    char local_ip[32];
    int32_t remote_port;
    char remote_ip[32];
    void* usr_data;
} KcpPeerConfig;

class KcpCtx
{
public:
    KcpCtx();
    ~KcpCtx();

    int PeerCreate(KcpPeerConfig config);
    static int KcpOutput(const char *buf, int len, ikcpcb *kcp, void *user);
    void SetInputCallback(std::function<void(void *, const char *, int)> callback) {
        m_InputCallback = callback;
    }
    int SendKcpData(const char* data, int size) {
		int wait_pack = ikcp_waitsnd(m_Kcpcb);
		if (wait_pack > m_MaxWaitPackets){
            return -1;
        }
        printf("wait pack:%d\n", wait_pack);
        return ikcp_send(m_Kcpcb, (char*)data, size);
    }
    int KcpSync();
    void Input(const char* data, int size);

private:
    int m_Socketfd;
    char* m_RecvBuf;
    int m_MaxWaitPackets;
    KcpPeerConfig m_PeerConfig;
    struct sockaddr_in m_LocalAddr;
    struct sockaddr_in m_RemoteAddr;
    ikcpcb *m_Kcpcb;
    std::function<void(void *, const char *, int)> m_InputCallback;
};

#endif /* KCP_COMMON_H */